import { Provide } from '@midwayjs/decorator';
// import axios from 'axios';
import privacy from '../privacy';
import * as md5 from 'md5'
import axios from 'axios';
import { MapDTO } from '../dto/map';
/**
 * 描述
 */
@Provide()
export class MapService {
    async getlocation(option: MapDTO) {
        let str = await this.createMapsign('/ws/geocoder/v1', {
            location: option.latitude + "," + option.longitude,
            key: privacy.map_key,
            get_poi: 1,
        })
        console.log(str);
        let result = await axios({
            url: "https://apis.map.qq.com/ws/geocoder/v1",
            params: {
                location: option.latitude + "," + option.longitude,
                key: privacy.map_key,
                get_poi: 1,
                sig: str
            }
        })
        // console.log("结果", result);
        return {
            code: 20000,
            a: result.data
        }
    }

    /**
  * 导航
  */
    async todaohang() {
        var option: any = {
            key: privacy.map_key,
            from: '22.72744,114.051758',
            // 114.051758,22.72744
            to: "22.72174,114.06031"

        }



        var sig = await this.createMapsign('/ws/direction/v1/driving/', option)
        option.sig = sig

        let { data } = await axios({
            url: 'https://apis.map.qq.com/ws/direction/v1/driving/',
            params: option
        })

        console.log(data);
        

        return {
            code: 20000,
            data: {
                "status": 0,
                "message": "Success",
                "request_id": "dd8ed7f2b3b74ee79b9f4bcb45a93083",
                "result": {
                  "routes": [
                    {
                      "mode": "DRIVING",
                      "distance": 3232,
                      "duration": 13,
                      "traffic_light_count": 0,
                      "toll": 0,
                      "restriction": { "status": 1 },
                      "polyline": [
                        22.727661, 114.051684, 82, 297, 15, 125, 0, 0, 572, -197, 566, -190,
                        137, -14, 60, 2, 55, 20, 42, 31, 0, 0, 47, 100, 68, 300, 41, 183, 0,
                        0, -136, 8, -259, 38, -134, 31, -230, 70, -251, 113, -177, 100,
                        -884, 575, -317, 213, -418, 298, 0, 0, 58, 76, 685, -487, 782, -503,
                        310, -189, 145, -74, 216, -77, 0, 0, 231, -68, 265, -36, 247, 3,
                        255, 30, 156, 39, 234, 85, 230, 114, 277, 172, 262, 182, 123, 64,
                        177, 46, 181, 21, 0, 0, -14, 627, -77, 2265, 0, 0, -36, 26, -18, 22,
                        -9, 26, -73, 631, -44, 587, -130, 2469, 0, 0, -53, 85, -75, 56,
                        -101, 25, -102, 13, -325, -346, -760, -889, 0, 0, -106, -22, -2490,
                        1504, -567, 370, -414, 169, -693, 228, -84, 41, -85, 71, -157, 47,
                        -232, 319, -125, 77, -484, 222, -127, 58, -125, 57, -121, 55, -127,
                        58, 0, 0, -36, -79, -328, -715, 0, 0, -158, 46, -126, 48, -159, 44,
                        -126, 61, -404, 147, 0, 0, -292, -656, -197, -463, -59, -157
                      ],
                      "steps": [
                        {
                          "instruction": "沿内部道路向东行驶44米,左转然后立即向右前方行驶",
                          "polyline_idx": [0, 5],
                          "road_name": "内部道路",
                          "dir_desc": "东",
                          "distance": 44,
                          "act_desc": "左转",
                          "accessorial_desc": "然后立即向右前方行驶"
                        },
                        {
                          "instruction": "行驶166米,",
                          "polyline_idx": [6, 19],
                          "road_name": "",
                          "dir_desc": "",
                          "distance": 166,
                          "act_desc": "",
                          "accessorial_desc": ""
                        },
                        {
                          "instruction": "行驶62米,右转",
                          "polyline_idx": [20, 27],
                          "road_name": "",
                          "dir_desc": "",
                          "distance": 62,
                          "act_desc": "右转",
                          "accessorial_desc": ""
                        },
                        {
                          "instruction": "沿富澜路行驶349米,左转调头",
                          "polyline_idx": [28, 47],
                          "road_name": "富澜路",
                          "dir_desc": "",
                          "distance": 349,
                          "act_desc": "左转调头",
                          "accessorial_desc": ""
                        },
                        {
                          "instruction": "沿富澜路向西北行驶283米,直行进入主路",
                          "polyline_idx": [48, 61],
                          "road_name": "富澜路",
                          "dir_desc": "西北",
                          "distance": 283,
                          "act_desc": "直行",
                          "accessorial_desc": "进入主路"
                        },
                        {
                          "instruction": "沿富澜路行驶310米,右转不要进入内部路",
                          "polyline_idx": [62, 87],
                          "road_name": "富澜路",
                          "dir_desc": "",
                          "distance": 310,
                          "act_desc": "右转",
                          "accessorial_desc": "不要进入内部路"
                        },
                        {
                          "instruction": "沿观光路向东行驶297米,偏右转进入辅路",
                          "polyline_idx": [88, 93],
                          "road_name": "观光路",
                          "dir_desc": "东",
                          "distance": 297,
                          "act_desc": "偏右转",
                          "accessorial_desc": "进入辅路"
                        },
                        {
                          "instruction": "沿观光路辅路行驶390米,偏右转进入右转专用道",
                          "polyline_idx": [94, 107],
                          "road_name": "观光路辅路",
                          "dir_desc": "",
                          "distance": 390,
                          "act_desc": "偏右转",
                          "accessorial_desc": "进入右转专用道"
                        },
                        {
                          "instruction": "沿桂花路向西南行驶218米,左转",
                          "polyline_idx": [108, 121],
                          "road_name": "桂花路",
                          "dir_desc": "西南",
                          "distance": 218,
                          "act_desc": "左转",
                          "accessorial_desc": ""
                        },
                        {
                          "instruction": "沿桂花路行驶746米,右转",
                          "polyline_idx": [122, 153],
                          "road_name": "桂花路",
                          "dir_desc": "",
                          "distance": 746,
                          "act_desc": "右转",
                          "accessorial_desc": ""
                        },
                        {
                          "instruction": "沿沿河东路九巷向西南行驶90米,左转",
                          "polyline_idx": [154, 159],
                          "road_name": "沿河东路九巷",
                          "dir_desc": "西南",
                          "distance": 90,
                          "act_desc": "左转",
                          "accessorial_desc": ""
                        },
                        {
                          "instruction": "沿桂澜路行驶113米,右转",
                          "polyline_idx": [160, 171],
                          "road_name": "桂澜路",
                          "dir_desc": "",
                          "distance": 113,
                          "act_desc": "右转",
                          "accessorial_desc": ""
                        },
                        {
                          "instruction": "沿内部道路行驶144米,",
                          "polyline_idx": [172, 179],
                          "road_name": "内部道路",
                          "dir_desc": "",
                          "distance": 144,
                          "act_desc": "",
                          "accessorial_desc": ""
                        }
                      ],
                      "tags": [],
                      "taxi_fare": { "fare": 15 }
                    }
                  ]
                }
              }
        }

    }

    /**
     * 地点搜索
     */
    async placeSearch(keyword: any) {
        var option: any
        option = {
            url: "https://apis.map.qq.com/ws/place/v1/search",
            params: {
                boundary: "nearby(22.729222,114.047416,1000,1)",
                keyword: '银星科技大厦',
                orderby: "_distance",
                page_size: 20,
                key: privacy.map_key
            }
        }
        let str = await this.createMapsign('/ws/place/v1/search', option.params)
        option.params.sig = str
        console.log(option.params);

        let result = await axios(option)

        // console.log(result);
        console.log(result);


        return { code: 666, md: result.data }

    }

    /**
     *  解析地址truth_analy  
     * 
     */
    async truth_analy() {
        var option: any
        option = {
            url: "https://apis.map.qq.com/ws/smart_address/place_analy",
            params: {
                address: "广东佛山市高明区杨和镇广东省佛山市高明区杨和镇杨梅村桃花街2号科顺建筑材料有限公司",
                key: privacy.map_key
            }
        }
        let str = await this.createMapsign('/ws/smart_address/place_analy', option.params)
        option.params.sig = str
        console.log(option.params);

        let result = await axios(option)

        // console.log(result);
        console.log(result);


        return { code: 666, md: result.data }
    }


    /**
     * 创造签名
     */
    async createMapsign(url: string, obj: any) {
        //签名算法（把所有的非空的参数，按字典顺序ASCILL组合起来+key,然后md5加密，再把加密结果都转成大写的即可）
        //  将对象转化为数组进行排序
        //  将需要加入签名的 参数 放在一个对象 keys转化为一个数组   sort() 默认就是ASCILL 排列方式
        obj.key = privacy.map_key
        let arr = Object.keys(obj).sort()
        var stra = arr.reduce((v, e) => {
            v = v + e + '=' + obj[e] + "&"
            return v
        }, "")
        //  微信商户得到
        let str = url + "?" + stra.substring(0, stra.length - 1)


        //  MD5 加密再转大写
        var signValue = md5(str + privacy.map_secret_key);
        console.log("代价密", str + privacy.map_secret_key);

        console.log("md5", signValue);


        return signValue
    }


}
